package com.hualan.sfzy.service.mind.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.mind.TestProblem;
import com.hualan.sfzy.entity.mind.TestProblemSelect;
import com.hualan.sfzy.mapper.mind.TestProblemMapper;
import com.hualan.sfzy.mapper.mind.TestProblemSelectMapper;
import com.hualan.sfzy.service.BaseServiceImp;
import com.hualan.sfzy.service.mind.TestProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TestProblemServiceImpl extends BaseServiceImp<TestProblem> implements TestProblemService {
    @Autowired
    private TestProblemMapper testProblemMapper;

    @Autowired
    private TestProblemSelectMapper testProblemSelectMapper;

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, TestProblem testProblem) {
        PageHelper.startPage(pageNum, pageSize);
        List<TestProblem> list = testProblemMapper.select(testProblem);
        PageInfo<TestProblem> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    @Override
    @Transactional
    public int insertWithOptions(TestProblem testProblem) {
        int result = testProblemMapper.insert(testProblem);
        if (result > 0) {
            // Insert default options
            TestProblemSelect option1 = new TestProblemSelect(null, "1.我经常这样", testProblem.getProblemName(), testProblem.getId(), 0);
            TestProblemSelect option2 = new TestProblemSelect(null, "2.我偶尔这样", testProblem.getProblemName(), testProblem.getId(), 0);
            TestProblemSelect option3 = new TestProblemSelect(null, "3.我不偶尔这样", testProblem.getProblemName(), testProblem.getId(), 0);
            TestProblemSelect option4 = new TestProblemSelect(null, "4.我不经常这样", testProblem.getProblemName(), testProblem.getId(), 0);
            testProblemSelectMapper.insert(option1);
            testProblemSelectMapper.insert(option2);
            testProblemSelectMapper.insert(option3);
            testProblemSelectMapper.insert(option4);
        }
        return result;
    }
}